#include "MainHeader.h"

int LoadSet(GFXData* spritedata,PalData*Palettedata, sprite_entry* SpriteInfo,u8 SpriteSetNo){ 

long off =0;
int X,i,compare;
CompHeader thiscompheader;
//sprite_entry tempit[15]; 
u8 enemydat[15];
u8 destination[15];
u8 max=0;



	for(X = 0; X<15; X++) {
		enemydat[X] = 0x10;
		destination[X] = 0x00;
	}

	for(X = 0; X<15; X++) {
		Palettedata[X].RomPointer = Palettedata[X].MemDst= Palettedata[X].Size = 0;
	if(SSE2==false)		
		SpriteInfo[X].sprtype = SpriteInfo[X].sprdetail = 0;
	
		spritedata[X].RomPointer =spritedata[X].MemDst = 0;
		
	}
    if(SSE2==false){  
		fseek(GBA.ROM, (SpriteSetNo * 4) + 0x75F31C, SEEK_SET);
		fread(&off, sizeof(long), 1, GBA.ROM);
		fseek(GBA.ROM, off-0x8000000, SEEK_SET);
 
		for( i=0; i< 15; i++) {
			fread(&SpriteInfo[i].sprtype, 1, 1, GBA.ROM);
			fread(&SpriteInfo[i].sprdetail, 1, 1, GBA.ROM);
		}
	}
	compare = 0xFF;

	for(X = 0; X<15; X++) {

		if(SpriteInfo[X].sprtype == 0) {
			max = X;
			if( X != 0) {
				break;                                  // end this if any type but the first =0
			}else{
				max = X;
				continue;                                  //if first type =0 then continue
			}
		}

		enemydat[X] = SpriteInfo[X].sprtype;                  //type is written to enemydata
		destination[X] = (SpriteInfo[X].sprdetail & 0x7);   //3 least significant bits to destination

		if( SpriteInfo[X].sprdetail == compare)
			continue; //compare detail# to last detail#

		compare = SpriteInfo[X].sprdetail;                    //store last detail#

		if( SpriteInfo[X].sprdetail == 0x8)
			continue;      //if detail# = 8, dont load gfx & pals

		//gfx and palette loading
		spritedata[X].RomPointer= sprgfxpnt + (SpriteInfo[X].sprtype - 0x10) * 4;
		spritedata[X].MemDst= (SpriteInfo[X].sprdetail * 0x800);
		fseek(GBA.ROM, sprgfxpnt + (SpriteInfo[X].sprtype - 0x10) * 4, SEEK_SET);
		fread(&off, 4, 1, GBA.ROM);
		fseek(GBA.ROM, off-0x8000000, SEEK_SET);
		fread(&thiscompheader.check_ten, 1, 1, GBA.ROM);       //Check byte should be 0x10 for lz
		fread(&thiscompheader.l3, 1, 1, GBA.ROM);
		fread(&thiscompheader.l2, 1, 1, GBA.ROM);
		fread(&thiscompheader.l1, 1, 1, GBA.ROM);

		Palettedata[X].RomPointer= sprpalpnt + (SpriteInfo[X].sprtype - 0x10) * 4;
		Palettedata[X].MemDst= (SpriteInfo[X].sprdetail * 16);
		Palettedata[X].Size= (thiscompheader.l2 & 0xF8) * 2;

	}
	if(SSE){
   	SpriteSet.total = max;
	}else{
		maxsprite=max;
	}

	return 0;
}


int LoadPal(PalData* palinfo,sprite_entry* spriteset, long *Palette) {
	if(&palinfo==0 || &spriteset==0) return 0;
    u8 max=0;
	max=maxsprite;
	if(SSE==true)max=SpriteSet.total;
	long  addybuf=0;
	u16 transferpal[1024]={0};
    char osh[256]={0};
	int  x=0;
	memset(Palette,0,512);
	SpriteSet.usedPAL=0;
//	memset(&GBASprPal[128],0,sizeof(GBASprPal)-128);
//    memset(&SprPal,0,sizeof(SprPal));
   
	         
		  for( x = 0; x<max; x++) {
			  if (palinfo[x].RomPointer  != 0){
			
		if(x!=0 && palinfo[x].RomPointer ==0) break;
      
		fseek(GBA.ROM, palinfo[x].RomPointer , SEEK_SET);
		fread(&addybuf, 4, 1, GBA.ROM);
		fseek(GBA.ROM, addybuf-0x8000000, SEEK_SET);
		fread(&transferpal, 1, (palinfo[x].Size)*2, GBA.ROM);
         
//	if(paltransfer[x][1] == 0) continue;
		sprintf(osh,"Dst is: %X",palinfo[x].MemDst);
	
			if(128+(palinfo[x].MemDst  ) < 256)
				memcpy(&GBASprPal[128+(palinfo[x].MemDst  )  ], &transferpal, (palinfo[x].Size)*2);

		if(SSE) SpriteSet.usedPAL+=(palinfo[x].Size);
			  }
   
		  }
	GBA.DecodePal(GBASprPal, Palette, 16, 0);
   
  

	return 0;
}


int LoadSpriteToMem(GFXData* ginfo,sprite_entry* spriteset,u8* GraphicsBuffer, TileBuffer* tb) {
	u32 addybuf=0;
	u32 size=0;
	u16 i=0;
	u16 ii=0;
	u32 dst=0;
	u8 decompbuf[0x8000]= {0};
    SpriteSet.usedGFX=0;
	memset(&GraphicsBuffer[0x4000], 0, 0x4000);
   GBA.Reopen();
	for(i=0; i<15; i++) {
		dst=0x4000+(ginfo[i].MemDst );
		if(dst>=0x8000){
			//MessageBox(0,"Sprites are bad, said sprites will not be drawn.","ERROR",MB_OK);
			return 0;
		}
        if((i!=0) && ginfo[i].MemDst == 0) continue;
		if(ginfo[i].RomPointer  == 0)		continue;
		switch(GameEngine->isZM()){
				case 0:
					fseek(GBA.ROM, ginfo[i].RomPointer , SEEK_SET);
					fread(&addybuf, 4, 1, GBA.ROM);
					fseek(GBA.ROM, addybuf-0x8000000, SEEK_SET);
					fread(GBA.CompBuffer, 1, sizeof(GBA.CompBuffer), GBA.ROM);
					size = GBA.LZ77UnComp(GBA.CompBuffer, decompbuf);
			        
                    memcpy(&GraphicsBuffer[dst], decompbuf, size);
				break; 
				case 1:
                    
					fseek(GBA.ROM, ginfo[i].RomPointer, SEEK_SET);
					fread(&addybuf, 1, 4, GBA.ROM);
					fseek(GBA.ROM, addybuf-0x8000000, SEEK_SET);
			        //fread(&GraphicsBuffer[dst],size, 1,  GBA.ROM);
					size = MFSprSize[(spriteset[i].sprtype-0x10)<<1];
					fread(&decompbuf[dst],size, 1,  GBA.ROM);
					for(ii=0;ii<size;ii++)GraphicsBuffer[dst+ii]=decompbuf[dst+ii];
				
				
				break;
		}
		if(SSE==true) SpriteSet.usedGFX+=size;
         
	}
  
	tb->Load(GraphicsBuffer, 1024);
	return 0;
}





int MFLoadSet(GFXData* spritedata,PalData*Palettedata, sprite_entry* SpriteInfo,u8 SpriteSetNo)
{

	int ii=0, X=0, i=0;
	u8 enemydat[15]={0};
	u8 destination[15]={0};
	u32 off=0;
	if(SSE2==false){
	maxsprite = 0;

	for(ii=0; ii<15; ii++)
	{
		enemydat[ii]= 16;
		destination[ii]= 0;
	}

		for(X = 0; X<15; X++) {
			Palettedata[X].RomPointer  = Palettedata[X].MemDst = Palettedata[X].Size= 0;
			SpriteInfo[X].sprtype = SpriteInfo[X].sprdetail = 0;
			spritedata[X].RomPointer  =spritedata[X].MemDst  = 0;
			
		}	
		if(GBA.ROM){
		fseek(GBA.ROM, SpriteSetNo*4 + 0x79ADD8, SEEK_SET);
		fread(&off, sizeof(long), 1, GBA.ROM);
		fseek(GBA.ROM,off-0x8000000, SEEK_SET);
			for( i=0; i< 15; i++) {

				fread(&SpriteInfo[i].sprtype, 1, 1, GBA.ROM);
				fread(&SpriteInfo[i].sprdetail, 1, 1, GBA.ROM);
			}
		}

	}
	//	uint sprite_in[X].sprtype= sprite_in[ii].sprtype, sprdetail= sprite_in[ii].sprdetail, 
	int prevsprdetail= 255;
    maxsprite = 0;
	if(SSE==true)SpriteSet.total=0;
	for(X=0; X<15; X++)
	{
		maxsprite++;
		if(SSE==true)SpriteSet.total++;
		if((sprite_in[X].sprtype == 0) &&( X != 0)) break;
	
		enemydat[X]= sprite_in[X].sprtype;
		destination[X]= sprite_in[X].sprdetail & 8;	 // ???

		if((sprite_in[X].sprdetail != prevsprdetail) && (sprite_in[X].sprdetail != 8))
		{

		spritedata[X].RomPointer= 0x79A5D8 + (SpriteInfo[X].sprtype - 0x10) * 4;
		spritedata[X].MemDst= (SpriteInfo[X].sprdetail * 0x800);


 		Palettedata[X].RomPointer= 0x79A8D4 + (SpriteInfo[X].sprtype - 0x10) * 4;
		if(SpriteInfo[X].sprdetail&7)Palettedata[X].MemDst= (SpriteInfo[X].sprdetail * 16);

	    Palettedata[X].Size=  (MFSprSize[(SpriteInfo[X].sprtype-0x10)<<1]/128);
	}
		prevsprdetail= SpriteInfo[X].sprdetail;
	}

	


	return 0;
}

